package org.dromara.Nyt7StatisticsInsureSubRegion.service.impl;

import org.dromara.Nyt7PolicyInsurance.domain.Nyt7PolicyInsurance;
import org.dromara.Nyt7PolicyInsurance.mapper.Nyt7PolicyInsuranceMapper;
import org.dromara.utils.utils.FilterUtil;
import org.dromara.utils.utils.SubUtil;
import org.dromara.Nyt7StatisticsInsureSubRegion.domain.vo.Nyt7StatisticsInsureSubRegionVo;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.Nyt7StatisticsInsureSubRegion.domain.bo.Nyt7StatisticsInsureSubRegionBo;
import org.dromara.Nyt7StatisticsInsureSubRegion.service.INyt7StatisticsInsureSubRegionService;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统计分析_投保统计_数量统计_按地区的数量统计Service业务层处理
 *
 * @author yt
 * @date 2025-01-06
 */
@RequiredArgsConstructor
@Service
public class Nyt7StatisticsInsureSubRegionServiceImpl implements INyt7StatisticsInsureSubRegionService {

    private final Nyt7PolicyInsuranceMapper nyt7PolicyInsuranceMapper;

    /**
     * 分页查询统计分析_投保统计_数量统计_按地区的数量统计列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 统计分析_投保统计_数量统计_按地区的数量统计分页列表
     */
    @Override
    public TableDataInfo<Nyt7StatisticsInsureSubRegionVo> queryPageList(Nyt7StatisticsInsureSubRegionBo bo, PageQuery pageQuery) {

        List<Nyt7StatisticsInsureSubRegionVo> statisticsVos = getStatisticsVos(bo);
        // 创建 Page 对象
        Page<Object> build = pageQuery.build();
        // 创建 Page 对象
        Page<Nyt7StatisticsInsureSubRegionVo> page = new Page<>(build.getCurrent(), build.getSize());
        page.setRecords(statisticsVos);
        page.setTotal(statisticsVos.size());
        page.setRecords(statisticsVos);
        return TableDataInfo.build(page);
    }

    private List<Nyt7StatisticsInsureSubRegionVo> getStatisticsVos(Nyt7StatisticsInsureSubRegionBo bo) {

        List<Nyt7PolicyInsurance> nyt7PolicyInsurances = FilterUtil.getInsurances(bo, nyt7PolicyInsuranceMapper, "region");
        if (nyt7PolicyInsurances == null) return new ArrayList<>();
        // 计算总金额
        BigDecimal allSub = SubUtil.getAllSub(nyt7PolicyInsurances);

        List<Nyt7StatisticsInsureSubRegionVo> statisticsVos = new ArrayList<>();
        int x = 1;
        if (!nyt7PolicyInsurances.isEmpty()) {
            Map<String, Integer> nameMap = new HashMap<>();
            for (Nyt7PolicyInsurance nyt7PolicyInsurance : nyt7PolicyInsurances) {
                //补贴总金额
                BigDecimal total = nyt7PolicyInsurance.getTotalCentralFiscalSubsidy()
                    .add(nyt7PolicyInsurance.getTotalProvincialFiscalSubsidy())
                    .add(nyt7PolicyInsurance.getTotalMunicipalFiscalSubsidy())
                    .add(nyt7PolicyInsurance.getTotalDistrictAndCountyFiscalSubsidy())
                    .add(nyt7PolicyInsurance.getTotalOtherFiscalSubsidies());

                //补贴金额占比
                // 使用 BigDecimal 进行除法运算，并指定精度和舍入模式
                BigDecimal Ratio = total
                    .divide(allSub, 5, RoundingMode.HALF_UP);

                Nyt7StatisticsInsureSubRegionVo subRegionVo = new Nyt7StatisticsInsureSubRegionVo();
                subRegionVo.setId((long) x++);
                subRegionVo.setRegion(nyt7PolicyInsurance.getPlotDetailInfo().getCity());
                subRegionVo.setNextRegion(nyt7PolicyInsurance.getPlotDetailInfo().getDistrictCounty());
                subRegionVo.setSubsidyAmount(total);
                subRegionVo.setSubsidyRatio(Ratio);
                subRegionVo.setCreateTime(nyt7PolicyInsurance.getCreateTime());
                statisticsVos.add(subRegionVo);

            }
        }
        return statisticsVos;
    }

    /**
     * 查询符合条件的统计分析_投保统计_数量统计_按地区的数量统计列表
     *
     * @param bo 查询条件
     * @return 统计分析_投保统计_数量统计_按地区的数量统计列表
     */
    @Override
    public List<Nyt7StatisticsInsureSubRegionVo> queryList(Nyt7StatisticsInsureSubRegionBo bo) {
        return getStatisticsVos(bo);
    }
}
